package com.ruoyi.project.remake.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.remake.domain.dto.BorrowApprovalDto;
import com.ruoyi.project.remake.domain.dto.BorrowApprovalExamineDto;
import com.ruoyi.project.remake.domain.dto.CaseDetailDto;
import com.ruoyi.project.remake.domain.enmu.RemakeStatusState;
import com.ruoyi.project.remake.domain.query.BorrowApprovalQuery;
import com.ruoyi.project.system.domain.TDoctorUser;
import com.ruoyi.project.system.service.ITDoctorUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.remake.mapper.TBorrowApprovalMapper;
import com.ruoyi.project.remake.domain.TBorrowApproval;
import com.ruoyi.project.remake.service.ITBorrowApprovalService;

/**
 * 借阅审批Service业务层处理
 *
 * @author 孙山伟
 * @date 2020-12-23
 */
@Service
public class TBorrowApprovalServiceImpl implements ITBorrowApprovalService {
    @Autowired
    private TBorrowApprovalMapper tBorrowApprovalMapper;
    @Autowired
    private ITDoctorUserService doctorUserService;

    /**
     * 查询借阅审批
     *
     * @param id 借阅审批ID
     * @return 借阅审批
     */
    @Override
    public TBorrowApproval selectTBorrowApprovalById(Long id) {
        return tBorrowApprovalMapper.selectTBorrowApprovalById(id);
    }

    /**
     * 查询借阅审批列表
     *
     * @param tBorrowApproval 借阅审批
     * @return 借阅审批
     */
    @Override
    public List<TBorrowApproval> selectTBorrowApprovalList(TBorrowApproval tBorrowApproval) {
        return tBorrowApprovalMapper.selectTBorrowApprovalList(tBorrowApproval);
    }

    /**
     * 新增借阅审批
     *
     * @param tBorrowApproval 借阅审批
     * @return 结果
     */
    @Override
    public int insertTBorrowApproval(TBorrowApproval tBorrowApproval) {
        return tBorrowApprovalMapper.insertTBorrowApproval(tBorrowApproval);
    }

    /**
     * 修改借阅审批
     *
     * @param tBorrowApproval 借阅审批
     * @return 结果
     */
    @Override
    public int updateTBorrowApproval(TBorrowApproval tBorrowApproval) {
        return tBorrowApprovalMapper.updateTBorrowApproval(tBorrowApproval);
    }

    /**
     * 批量删除借阅审批
     *
     * @param ids 需要删除的借阅审批ID
     * @return 结果
     */
    @Override
    public int deleteTBorrowApprovalByIds(Long[] ids) {
        return tBorrowApprovalMapper.deleteTBorrowApprovalByIds(ids);
    }

    /**
     * 删除借阅审批信息
     *
     * @param id 借阅审批ID
     * @return 结果
     */
    @Override
    public int deleteTBorrowApprovalById(Long id) {
        return tBorrowApprovalMapper.deleteTBorrowApprovalById(id);
    }

    @Override
    public List<TBorrowApproval> selectFrontApprovalList(BorrowApprovalDto tBorrowApproval) {
        return tBorrowApprovalMapper.selectFrontApprovalList(tBorrowApproval);
    }

    @Override
    public AjaxResult frontCreatBorrowApproval(BorrowApprovalDto tBorrowApproval) {

        TBorrowApproval approval = new TBorrowApproval();

        approval.setApplyDoctor(String.valueOf(tBorrowApproval.getApplyDoctor()));
        approval.setApplyTime(new Date());
        approval.setApprovalStatus(RemakeStatusState.WAIT_AUDIT.getValue().toString());
        approval.setApplyDoctor(tBorrowApproval.getApplyDoctor().toString());
        TDoctorUser doctorUser= doctorUserService.selectTDoctorUserById(tBorrowApproval.getApplyDoctor());
        if(null==doctorUser){
          return   AjaxResult.error("医生信息不存在");
        }
        approval.setApplyDoctorName(doctorUser.getNickName());
        approval.setBorrowEndTime(tBorrowApproval.getBorrowEndTime());
        approval.setBorrowStartTime(tBorrowApproval.getBorrowStartTime());
        approval.setCardNumber(tBorrowApproval.getCarNumber());
        approval.setClassMainId(tBorrowApproval.getCaseMainId());
        approval.setBorrowObjective(tBorrowApproval.getBorrowObjective());
        approval.setClassType(tBorrowApproval.getClassType());
        int falg = tBorrowApprovalMapper.insertTBorrowApproval(approval);

        return falg == 1 ? AjaxResult.success() : AjaxResult.error("保存失败");
    }

    @Override
    public List<TBorrowApproval> selectAllFrontApprovalList(BorrowApprovalQuery borrowApprovalQuery) {
        return tBorrowApprovalMapper.selectAllFrontApprovalList(borrowApprovalQuery);
    }

    @Override
    public AjaxResult examine(BorrowApprovalExamineDto borrowApprovalExamineDto) {
        TBorrowApproval check=selectTBorrowApprovalById(borrowApprovalExamineDto.getId());
        if(!check.getApprovalStatus().equals(RemakeStatusState.WAIT_AUDIT.getValue().toString())){
            return AjaxResult.error("不是待审核状态，不允许操作");
        }
        //审核不通过，则审核原因不允许为空
        if(borrowApprovalExamineDto.getApprovalStatus().equals(RemakeStatusState.NO_PASS_AUDIT.getValue().toString())){
            if(StringUtils.isEmpty(borrowApprovalExamineDto.getRefuse())){
                return AjaxResult.error("审核原因不允许为空");
            }
        }
        TBorrowApproval tBorrowApproval=new TBorrowApproval();
        tBorrowApproval.setId(borrowApprovalExamineDto.getId());
        tBorrowApproval.setApplyTime(new Date());
        tBorrowApproval.setApprovalStatus(borrowApprovalExamineDto.getApprovalStatus());
        tBorrowApproval.setRefuse(borrowApprovalExamineDto.getRefuse());
        updateTBorrowApproval(tBorrowApproval);
        return AjaxResult.success();
    }
}
